package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.RUV;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.model.dto.SetmealDto;
import com.itheima.reggie.model.entity.Setmeal;
import com.itheima.reggie.model.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RUV(name = "多表CRUD", techHighlights = "涉及到对多张表的操作，值得学习", labelJson = "Java,多表操作", remark = "啦啦啦啦")
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

//    @Autowired
//    private SetmealMapper setmealMapper;


    /**
     * 新增套餐，同时保存套餐和菜品的关联信息
     * @param setmealDto
     */
    @Transactional //加上事务注解：因为操作两张表，要么全成功，要么全失败
    public void savaWithDish(SetmealDto setmealDto){
        //操作setmeal表，执行insert操作，保存套餐的基本信息
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //依次遍历 将setmeal_id赋到setmeal_dish中
        setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //操作setmeal_dish，执行insert操作，保存套餐和菜品的关联信息，
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联信息
     * @param ids
     */
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);

        if (count > 0){
            //如果不能删除，抛出一个业务异常，直接就返回了，下面的代码就不在执行了
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除
        //先删除关系表中的数据--setmeal_dish
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        //批量删除
        setmealDishService.remove(lambdaQueryWrapper);

        //再删除套餐表中的数据--setmeal批量删除
        this.removeByIds(ids);

//        //由ids得到setmeals
//        List<Setmeal> setmeals = this.listByIds(ids);
//
//        //依次遍历每个setmeal
//        setmeals.stream().map((item) -> {
//            //若套餐停售，则可进行删除
//            if (item.getStatus()==0){
//                ////删除setmeal_dish表中对应的套餐菜品信息
//                //1 从setmeal中拿到setmealId
//                Long setmealId = item.getId();
//                //2 添加删除条件
//                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
//                wrapper.eq(setmealId!=null,SetmealDish::getSetmealId,setmealId);
//                //3 删除setmeal_dish表中对应的套餐菜品信息
//                setmealDishService.remove(wrapper);
////                List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
//
//                ////再删除setmeal表中套餐信息
//                this.removeById(setmealId);
//            }else if (item.getStatus()==1){//若套餐停售，则需先进行停售
//                log.info("套餐{}停售，需先进行停售",item.getName());
//            }
//            return item;
//        }).collect(Collectors.toList());
    }
}
